package com.samtech.spring.components;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.samtech.domain.EtAirport;
import com.samtech.primefaces.service.EtAirportService;







@Component("createEtairport")
@Scope("request")
public class CreateEtairport implements Serializable{
 
	private EtAirport etairport = new EtAirport();
	
	private EtAirportService etAirportService;
	
	private String airportCode;
	
	
	public CreateEtairport() {}
	
	@Autowired
	public CreateEtairport(EtAirportService etAirportService) {
		this.etAirportService = etAirportService;
	}
	
	public void init(){
		FacesContext cxt = FacesContext.getCurrentInstance();
		ExternalContext extCxt = cxt.getExternalContext();
		Object request = extCxt.getRequest();
		if(request!=null && request instanceof HttpServletRequest){
			HttpServletRequest  r=(HttpServletRequest)request;
			String method = r.getMethod();
			if(method.equalsIgnoreCase("get")){
				String airport = r.getParameter("airportCode");
				if(airport!=null){
					EtAirport e = etAirportService.findById(airport);
					this.setEtAirport(e);
				}
			}
		}
	}
	
	public boolean isInited(){
		init();
		return false;
	}
	
	public String getAirportCode() {
		return airportCode;
	}

	public void setAirportCode(String airportCode) {
		this.airportCode = airportCode;
	}

	public EtAirport getEtAirport() {
		return etairport;
	}
	
	public void setEtAirport(EtAirport et) {
		this.etairport = et;
	}
	
	public void save(ActionEvent actionEvent) {
		etAirportService.createNew(etairport);
		FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Etairport is saved");
		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		etairport = new EtAirport();
	}
	public void update(ActionEvent actionEvent) {
		etAirportService.update(etairport);
		FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Etairport is updated");
		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		//etairport = new EtAirport();
	}
}